package com.hooker.adapter

import android.content.Context
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hooker.bean.PackageInfo
import com.hooker.bean.TypeInfo
import com.hooker.hook.Rule
import com.hooker.util.IO
import com.sdk.hooker.penetration.R
import org.json.JSONArray

class RuleListAdapter : RecyclerView.Adapter<RuleListAdapter.RuleHolder> {

    var context: Context? = null
    var list: MutableList<Rule> = mutableListOf()
    private var listView: RecyclerView? = null
    lateinit var mClickListener: OnItemClickListener

    constructor(context: Context, list: MutableList<Rule>, view: RecyclerView) {
        this.context = context
        this.list.addAll(list)
        this.listView = view
    }

    fun update(data: MutableList<Rule>) {
        this.list.clear()
        this.list.addAll(data)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.mClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RuleHolder {
        var view: View = View.inflate(context, R.layout.layout_rule_item, null)
        view.layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        return RuleHolder(view)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: RuleHolder, position: Int) {
        var info = list?.get(position)

        holder.ruleName?.text = info?.name

        holder.ruleClass?.text = info?.className

        holder.ruleMethod.text = info.methodWithParam

        holder.itemView.setTag(R.id.position, position)
        holder.itemView.tag = info

        holder.itemView.setOnClickListener { view ->
            var pos = view.getTag(R.id.position) as Int

            mClickListener?.onClick(view, pos)
        }

        holder.ruleDelete.setTag(R.id.position, position)
        holder.ruleDelete.tag = info
        holder.ruleDelete.setOnClickListener { view ->
            var pos = view.getTag(R.id.position) as Int

            mClickListener?.onClick(view, pos)
        }
    }

    class RuleHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var ruleName: TextView? = null
        var ruleClass: TextView? = null
        var ruleMethod: TextView
        var ruleDelete: Button

        init {
            ruleName = itemView.findViewById(R.id.tv_rule_name)
            ruleClass = itemView.findViewById(R.id.tv_rule_class)
            ruleMethod = itemView.findViewById(R.id.tv_rule_method)
            ruleDelete = itemView.findViewById(R.id.btn_rule_delete)
        }
    }

    interface OnItemClickListener {
        fun onClick(view: View, position: Int)
    }
}